/*
 * Copyright (C) Yumin Wu <wuyuminst@163.com>
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "rk_ispv4l2.h"

#if 0
#define DBG(...) do { fprintf(stderr, __VA_ARGS__); } while (0)
#else
#define DBG(...) do {} while (0)
#endif

#define DEV_NODE "/dev/video19"
#define OUT_FILE "/data/capture.yuv"

#define WIDTH  1920
#define HEIGHT 1080

static int i;
static int skipCnt = 30;
static int outputCnt = 3;
static int wCnt = 0;
static FILE *fp;

void demo_callback(int src_fd, const void *ptr, unsigned int size, int w, int h)
{
	DBG("%s: w = %d, h = %d\n", __func__, w, h);
	if(i > skipCnt) {
		if(wCnt < outputCnt && fp) {
			printf(">\n");
			fwrite(ptr, size, 1, fp);
			fflush(fp);
			wCnt++;
		}
	} else {
		i++;
	}
}

int main(int argc, char **argv)
{
	int ret;
	printf("rk_ispv4l2: demo begin\n");

	fp = fopen(OUT_FILE, "w+");
	if(fp == NULL) {
    	printf("%s: open out_file error!\n", __func__);
    	exit(EXIT_FAILURE);
    }

	ret = rk_ispv4l2_init(DEV_NODE, demo_callback, WIDTH, HEIGHT);
	if(ret) {
		printf("%s: rk_ispv4l2_init failed\n", __func__);
		exit(EXIT_FAILURE);
	}

	sleep(5);

	if(fp)
		fclose(fp);
	rk_ispv4l2_exit();

	printf("rk_ispv4l2: demo end\n");

	return 0;
}
